1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.collect;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import java.util.Collection;
20  import java.util.Comparator;
21  
22  import javax.annotation.Nullable;
23  
24  /**
25   * An empty immutable sorted multiset.
26   *
27   * @author Louis Wasserman
28   */
29  @SuppressWarnings("serial") // Uses writeReplace, not default serialization
30  final class EmptyImmutableSortedMultiset<E> extends ImmutableSortedMultiset<E> {
31    private final ImmutableSortedSet<E> elementSet;
32  
33    EmptyImmutableSortedMultiset(Comparator<? super E> comparator) {
34      this.elementSet = ImmutableSortedSet.emptySet(comparator);
35    }
36  
37    @Override
38    public Entry<E> firstEntry() {
39      return null;
40    }
41  
42    @Override
43    public Entry<E> lastEntry() {
44      return null;
45    }
46  
47    @Override
48    public int count(@Nullable Object element) {
49      return 0;
50    }
51  
52    @Override
53    public boolean containsAll(Collection<?> targets) {
54      return targets.isEmpty();
55    }
56  
57    @Override
58    public int size() {
59      return 0;
60    }
61  
62    @Override
63    public ImmutableSortedSet<E> elementSet() {
64      return elementSet;
65    }
66  
67    @Override
68    Entry<E> getEntry(int index) {
69      throw new AssertionError("should never be called");
70    }
71  
72    @Override
73    public ImmutableSortedMultiset<E> headMultiset(E upperBound, BoundType boundType) {
74      checkNotNull(upperBound);
75      checkNotNull(boundType);
76      return this;
77    }
78  
79    @Override
80    public ImmutableSortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType) {
81      checkNotNull(lowerBound);
82      checkNotNull(boundType);
83      return this;
84    }
85  
86    @Override
87    public UnmodifiableIterator<E> iterator() {
88      return Iterators.emptyIterator();
89    }
90  
91    @Override
92    public boolean equals(@Nullable Object object) {
93      if (object instanceof Multiset) {
94        Multiset<?> other = (Multiset<?>) object;
95        return other.isEmpty();
96      }
97      return false;
98    }
99  
100   @Override
101   boolean isPartialView() {
102     return false;
103   }
104 
105   @Override
106   int copyIntoArray(Object[] dst, int offset) {
107     return offset;
108   }
109 
110   @Override
111   public ImmutableList<E> asList() {
112     return ImmutableList.of();
113   }
114 }